#pragma once
#include<jsoncpp/json/json.h>
#include<iostream>
#include<string>
#include"Log.hpp"

using namespace LogMudule;



//序列化
bool Serialization(const Json::Value& val, std::string *out)
{
    //1.创建StreamWriterBuilder对象
    Json::StreamWriterBuilder swb;

    //2.获取StreamWriter对象
    Json::StreamWriter *sw = swb.newStreamWriter();

    //3.使用write进行序列化
    std::stringstream ss;
    int n = sw->write(val,&ss);
    if(n != 0)
    {
        LOG(LogLevel::ERROR)<<"write erron ...";
        return false;
    }
    *out = ss.str();

    delete sw;
    return true;
}


//反序列化
bool Deserialization(const std::string &in, Json::Value* val)
{
    //1.创建CharReaderBuilder对象
    Json::CharReaderBuilder crb;

    //2.获取CharReader对象
    Json::CharReader * cr = crb.newCharReader();

    //3.使用parse进行序列化
    bool res = cr->parse(in.c_str(),in.c_str()+in.size(),val,nullptr);
    if(!res)
    {
        LOG(LogLevel::ERROR)<<"parse erron ...";
        return false;
    }

    delete cr;
    return true;
}